package com.ruoyi.system.service.aggregation.impl;

import java.io.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.QrCodeUtil;
import com.ruoyi.system.domain.aggregation.User;
import com.ruoyi.system.mapper.aggregation.UserMapper;
import com.ruoyi.system.service.aggregation.IQrCodeService;

/**
 * 二维码Service实现
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@Service
public class QrCodeServiceImpl implements IQrCodeService 
{
    @Autowired
    private UserMapper userMapper;

    /**
     * 生成商家聚合二维码
     */
    @Override
    public String generateMerchantQrCode(Long merchantId) throws Exception
    {
        // 查询商家信息
        User merchant = userMapper.selectUserById(merchantId);
        if (merchant == null)
        {
            throw new Exception("商家不存在");
        }

        // 二维码内容：商家ID（前端扫码后根据merchantId查询商品）
        String content = "merchant://" + merchantId;

        // 生成二维码文件
        String uploadPath = RuoYiConfig.getUploadPath();
        String fileName = "qrcode/merchant_" + merchantId + "_" + System.currentTimeMillis() + ".png";
        String filePath = uploadPath + File.separator + fileName;

        // 创建目录
        File dir = new File(uploadPath + File.separator + "qrcode");
        if (!dir.exists())
        {
            dir.mkdirs();
        }

        // 生成二维码
        QrCodeUtil.generateQRCodeFile(content, filePath, 400, 400);

        return fileName;
    }

    /**
     * 生成App下载二维码
     */
    @Override
    public String generateDownloadQrCode() throws Exception
    {
        // 二维码内容：App下载地址（可配置）
        String downloadUrl = "https://example.com/app/download";
        String content = downloadUrl;

        // 生成二维码文件
        String uploadPath = RuoYiConfig.getUploadPath();
        String fileName = "qrcode/app_download.png";
        String filePath = uploadPath + File.separator + fileName;

        // 创建目录
        File dir = new File(uploadPath + File.separator + "qrcode");
        if (!dir.exists())
        {
            dir.mkdirs();
        }

        // 生成二维码
        QrCodeUtil.generateQRCodeFile(content, filePath, 400, 400);

        return fileName;
    }
}

